package com.qf.admin.dao.impl;

import com.qf.admin.dao.PayDao;
import com.qf.admin.domain.Orders;
import com.qf.admin.domain.Pay;
import com.qf.commons.TableData;
import com.qf.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PayDaoImpl implements PayDao {
    QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
    @Override
    public TableData<Pay> getAllPay(Integer limit, Integer offset) {

        TableData<Pay> tableData = new TableData<Pay>();

//        StringBuffer getPaySql = new StringBuffer("SELECT p.trade_status trade_status,p.pay_id id,a.a_name name,a.a_phone phone,p.order_no order_on,");
//        getPaySql.append("p.pay_info pay_info,p.trade_no trade_no,p.total_amount total_amount,p.update_time create_time FROM pay p INNER JOIN order_address o ");
//        getPaySql.append("ON p.order_no = o.o_no INNER JOIN address a ON o.o_address = a.a_id where 1 = 1");

        List<Object> paramsList = new ArrayList<>();

        String getAllPaySql = "SELECT p.trade_status trade_status,p.pay_id id,a.a_name name,a.a_phone phone,p.order_no order_on," +
                "p.pay_info pay_info,p.trade_no trade_no,p.total_amount total_amount,p.update_time create_time FROM pay p INNER JOIN order_address o " +
                "ON p.order_no = o.o_no INNER JOIN address a ON o.o_address = a.a_id limit ?,? ";


        String payTotalSql ="select count(1) from pay p INNER JOIN order_address o ON p.order_no = o.o_no INNER JOIN address a ON o.o_address = a.a_id ";



        try {
            List<Pay> allPay = queryRunner.query(getAllPaySql, new BeanListHandler<Pay>(Pay.class),offset,limit);
            Long total1 = queryRunner.query(payTotalSql, new ScalarHandler<>());
            Integer total = total1.intValue();

            tableData.setRows(allPay);
            tableData.setTotal(total);
            return tableData;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }





    @Override
    public void deletePay(Integer userID) {
        String deletePaySql = "delete from pay where pay_id = ? ";
        try {
            queryRunner.update(deletePaySql,userID);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public void updatePay(Integer userId) {
        String updatePaySql = "update pay set trade_status = 'TRADE_FAIL' where pay_id = ?";
        try {
            queryRunner.update(updatePaySql,userId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}
